package com.personalblog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personalblog.po.Tag;
import com.personalblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.sql.ResultSet;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/23 - 15:17
 */
@Controller
@RequestMapping("/admin")
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/tags")
    public String tags(@RequestParam(defaultValue = "1") int page, Model model){

        //引入分页查询，使用PageHelper分页功能,顺序不能乱，查询一定要在后面
        //在查询之前传入当前页，然后每页多少记录
        PageHelper.startPage(page,5);
        List tagList = tagService.findTags();
        PageInfo pageInfo = new PageInfo(tagList);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println("查询tag数目"+tagList.size());
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public String tagInput(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }


    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable int id,Model model){
        model.addAttribute("tag",tagService.findTagById(id));
        return "admin/tags-input";
    }
    @PostMapping("/tags")
    public String post(Tag tag, BindingResult bindingResult, RedirectAttributes attributes){
        Tag tag1 = tagService.findTagByName(tag.getTagName());
        if (tag1 != null){
            bindingResult.rejectValue("name","nameError","不能重复添加标签");
        }
        if(bindingResult.hasErrors()){
            return "admin/tags-input";
        }
        int i = tagService.insertTag(tag);
        if (i == 0){
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/tags";
    }

    @PostMapping("/tags/{id}")
    public String editPost(Tag tag, @PathVariable int id, BindingResult bindingResult, RedirectAttributes attributes){
        Tag tag1 = tagService.findTagByName(tag.getTagName());
        if (tag1 != null){
            bindingResult.rejectValue("name","nameError","不能重复添加标签");
        }
        if(bindingResult.hasErrors()){
            return "admin/tags-input";
        }
        int i = tagService.updateTag(id,tag);
        if (i == 0){
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable int id,RedirectAttributes attributes){
        int i = tagService.deleteTag(id);
        if (i == 0){
            attributes.addFlashAttribute("message","删除失败");
        }else {
            attributes.addFlashAttribute("message","删除成功");
        }
        return "redirect:/admin/tags";
    }


    @GetMapping("/tagListIds")
    @ResponseBody
    public String tagListIds(){
        return tagService.findTagListByIds("1,2,3").toString();
    }

}
